﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BTGISWASECO.DataLayer;
using BTGISWASECO.BusinessEntities;
using BTGISWASECO.BusinessLayer;

public partial class PresentationLayer_DefaultTab : System.Web.UI.Page
{
    OngNhanhBLL bll = new OngNhanhBLL();
    DMHuongDongChayDAL DMHuongDongChayDal = new DMHuongDongChayDAL();
    OngNhanhEntities OngNhanhEntities = new OngNhanhEntities();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["DataTable"] == "")
                Response.Redirect("../default.aspx");
            for (int i = DateTime.Now.Year; i >= 1990; --i)
            {
                ListItem iteml = new ListItem(i.ToString(), i.ToString());
                DListNamSx.Items.Add(iteml);
                DListNamLD.Items.Add(iteml);
            }
            DListNamSx.SelectedValue = OngNhanhEntities.NamSanXuat.ToString();
            DListNamLD.SelectedValue = OngNhanhEntities.NamLapDat.ToString();
            OngNhanhEntities = bll.GetOngNhanhEntitiesByID();
            txtObjectID.Value = OngNhanhEntities.OBJECTID.ToString();
            txtMaDoanOng.Text = OngNhanhEntities.IDDoanOng;
            this.txtDMA.Text = OngNhanhEntities.IDDMA;
            this.txtDMZ.Text = OngNhanhEntities.IDDMZ;
            foreach (var itemc in DMHuongDongChayDAL.GetList())
            {
                if (OngNhanhEntities.HuongDongChayText.Trim() == itemc.MoTa.ToString().Trim())
                    OngNhanhEntities.HuongDongChay = itemc.Ma;
                ListItem iteml = new ListItem(itemc.MoTa.ToString(), itemc.Ma.ToString());
                DListHDChay.Items.Add(iteml);
            }

            DListHDChay.SelectedValue = OngNhanhEntities.HuongDongChay.ToString();
            // this.DListHDChay.DataSource = DMHuongDongChayDal.GetDMHuongDongChayEntitiesByID();
            this.nChieuDai.Text = OngNhanhEntities.ChieuDai.ToString();
            this.nDKNgoai.Text = OngNhanhEntities.DKNgoai.ToString();
            this.nDKTrong.Text = OngNhanhEntities.DKTrong.ToString();
            this.nSLKPSuCo.Text = OngNhanhEntities.SoLanSuCo.ToString();
            this.txtNguoiCNC.Text = OngNhanhEntities.NguoiCNCuoi;
            this.txtNgayCNC.Text = OngNhanhEntities.NgayCNCuoi.ToString("dd/MM/yyyy");
            this.txtDoNhamDD.Text = OngNhanhEntities.DoNhamDanhDinh.ToString();
            this.txtDoNhamTT.Text = OngNhanhEntities.DoNhamThucTe.ToString();
            this.dApLucHD.Text = OngNhanhEntities.ALHoatDong.ToString();
            this.dApLucTK.Text = OngNhanhEntities.ApLucThietKe.ToString();
            this.txtDiemCuoi.Text = OngNhanhEntities.DiemDau;
            this.txtDiemDau.Text = OngNhanhEntities.DiemCuoi;
            this.dDoSau.Text = OngNhanhEntities.DoSau.ToString();
            this.dDoDoc.Text = OngNhanhEntities.DoDoc.ToString();
            this.txtMaHinhAnh.Text = OngNhanhEntities.IDHinhAnh;
            this.txtGhiChu.Text = OngNhanhEntities.GhiChu;
            //DateTime dob = DateTime.Parse(Request.Form[txtNgayCNC.UniqueID]); DMLoaiDonVi
            foreach (var itemk in DMKichCoOngDAL.GetList())
            {
                if (OngNhanhEntities.CoOngText.ToString().Trim() == itemk.MoTa.ToString().Trim())
                    OngNhanhEntities.CoOng = itemk.Ma;
                ListItem iteml = new ListItem(itemk.MoTa.ToString(), itemk.Ma.ToString());
                DListKCOng.Items.Add(iteml);
            }
            if (OngNhanhEntities.CoOng != null)
            DListKCOng.SelectedValue = OngNhanhEntities.CoOng.ToString();

            foreach (var itemvl in DMVatLieuDAL.GetList())
            {
                if (OngNhanhEntities.VatLieuText.ToString().Trim() == itemvl.MoTa.ToString().Trim())
                    OngNhanhEntities.VatLieu = itemvl.Ma;
                ListItem iteml = new ListItem(itemvl.MoTa.ToString(), itemvl.Ma.ToString());
                DListVatLieu.Items.Add(iteml);
            }
            DListVatLieu.SelectedValue = OngNhanhEntities.VatLieu.ToString();

            foreach (var itemtc in DMTieuChuanOngNuocDAL.GetList())
            {
                if (OngNhanhEntities.TieuChuanVLText.ToString().Trim() == itemtc.MoTa.ToString().Trim())
                    OngNhanhEntities.TieuChuanVL = itemtc.Ma;
                ListItem iteml = new ListItem(itemtc.MoTa.ToString(), itemtc.Ma.ToString());
                DListTCVatLieu.Items.Add(iteml);
            }
            DListTCVatLieu.SelectedValue = OngNhanhEntities.TieuChuanVL.ToString();

            foreach (var itemnsx in DMNhaSanXuatDAL.GetList())
            {
                if (OngNhanhEntities.NhaSanXuatText.ToString().Trim() == itemnsx.MoTa.ToString().Trim())
                    OngNhanhEntities.NhaSanXuat = itemnsx.Ma;
                ListItem iteml = new ListItem(itemnsx.MoTa.ToString(), itemnsx.Ma.ToString());
                DListNhaSX.Items.Add(iteml);
            }
            DListNhaSX.SelectedValue = OngNhanhEntities.NhaSanXuat.ToString();

            foreach (var itemnusx in DMNuocSanXuatDAL.GetList())
            {
                if (OngNhanhEntities.NuocSanXuatText.ToString().Trim() == itemnusx.MoTa.ToString().Trim())
                    OngNhanhEntities.NuocSanXuat = itemnusx.Ma;
                ListItem iteml = new ListItem(itemnusx.MoTa.ToString(), itemnusx.Ma.ToString());
                DListNuocSX.Items.Add(iteml);
            }
            DListNuocSX.SelectedValue = OngNhanhEntities.NuocSanXuat.ToString();

            foreach (var itemnttsd in DMTinhTrangSDDAL.GetList())
            {
                if (OngNhanhEntities.TinhTrangText.ToString().Trim() == itemnttsd.MoTa.ToString().Trim())
                    OngNhanhEntities.TinhTrang = itemnttsd.Ma;
                ListItem iteml = new ListItem(itemnttsd.MoTa.ToString(), itemnttsd.Ma.ToString());
                DListTTSD.Items.Add(iteml);
            }
            DListTTSD.SelectedValue = OngNhanhEntities.TinhTrang.ToString();

            foreach (var itemntldv in DMLoaiDonViDAL.GetList())
            {
                if (OngNhanhEntities.DonViTKText.ToString().Trim() == itemntldv.MoTa.ToString().Trim())
                    OngNhanhEntities.DonViTK = itemntldv.Ma;
                ListItem iteml = new ListItem(itemntldv.MoTa.ToString(), itemntldv.Ma.ToString());
                DListDonViTK.Items.Add(iteml);
            }
            DListDonViTK.SelectedValue = OngNhanhEntities.DonViTK.ToString();

            foreach (var itemntldvtc in DMLoaiDonViDAL.GetList())
            {
                if (OngNhanhEntities.DonViTCText != null)
                    if (OngNhanhEntities.DonViTCText.Trim() == itemntldvtc.MoTa.ToString().Trim())
                        OngNhanhEntities.DonViTC = itemntldvtc.Ma;
                ListItem iteml = new ListItem(itemntldvtc.MoTa.ToString(), itemntldvtc.Ma.ToString());
                DListDonViTC.Items.Add(iteml);
            }
            DListDonViTC.SelectedValue = OngNhanhEntities.DonViTC.ToString();

            foreach (var itemntldvgs in DMLoaiDonViDAL.GetList())
            {
                if (OngNhanhEntities.DonViGSText != null)
                    if (OngNhanhEntities.DonViGSText.Trim() == itemntldvgs.MoTa.ToString().Trim())
                        OngNhanhEntities.DonViGS = itemntldvgs.Ma;
                ListItem iteml = new ListItem(itemntldvgs.MoTa.ToString(), itemntldvgs.Ma.ToString());
                DListDonViGS.Items.Add(iteml);
            }
            DListDonViGS.SelectedValue = OngNhanhEntities.DonViGS.ToString();
        }
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        OngNhanhEntities bk = new OngNhanhEntities();
        OngNhanhDAL OngNhanhDAL = new OngNhanhDAL();
        //OngNhanhEntities OngNhanhEntities = new OngNhanhEntities(Convert.ToInt32(DListDonViTC.SelectedValue = DListDonViTC.SelectedValue!=""?DListDonViTC.SelectedValue:"0"),
        //    Convert.ToInt32(DListDonViGS.SelectedValue = DListDonViGS.SelectedValue!=""?DListDonViGS.SelectedValue:"0"), this.txtNguoiCNC.Text, DateTime.Parse(this.txtNgayCNC.Text.ToString()), this.txtGhiChu.Text, this.txtMaHinhAnh.Text);
        OngNhanhEntities OngNhanhEntities = new OngNhanhEntities(Convert.ToInt32(txtObjectID.Value), this.txtMaDoanOng.Text,
           this.txtDMZ.Text, this.txtDMA.Text, Convert.ToInt32(DListHDChay.SelectedValue = DListHDChay.SelectedValue != "" ? DListHDChay.SelectedValue : "0"), Convert.ToDouble(this.nChieuDai.Text),
           Convert.ToDouble(this.nDKTrong.Text), Convert.ToDouble(this.nDKNgoai.Text), DListKCOng.SelectedValue = DListKCOng.SelectedValue != "" ? DListKCOng.SelectedValue : "",
           Convert.ToInt32(DListVatLieu.SelectedValue = DListVatLieu.SelectedValue != "" ? DListVatLieu.SelectedValue : "0"), Convert.ToInt32(DListTCVatLieu.SelectedValue = DListTCVatLieu.SelectedValue != "" ? DListTCVatLieu.SelectedValue : "0"),
           Convert.ToInt32(DListNhaSX.SelectedValue = DListNhaSX.SelectedValue != "" ? DListNhaSX.SelectedValue : "0"), Convert.ToInt32(DListNuocSX.SelectedValue = DListNuocSX.SelectedValue != "" ? DListNuocSX.SelectedValue : "0"),
           Convert.ToInt32(DListNamSx.SelectedValue), Convert.ToInt32(DListNamLD.SelectedValue), Convert.ToDouble(this.txtDoNhamDD.Text), Convert.ToDouble(this.txtDoNhamTT.Text),
           Convert.ToDouble(this.dApLucHD.Text), Convert.ToDouble(this.dApLucTK.Text),
           this.txtDiemDau.Text, this.txtDiemCuoi.Text, Convert.ToDouble(this.dDoSau.Text),
           Convert.ToDouble(this.dDoDoc.Text), Convert.ToInt32(DListTTSD.SelectedValue = DListTTSD.SelectedValue != "" ? DListTTSD.SelectedValue : "0"),
           Convert.ToInt32(this.nSLKPSuCo.Text), Convert.ToInt32(DListDonViTK.SelectedValue = DListDonViTK.SelectedValue != "" ? DListDonViTK.SelectedValue : "0"),
           Convert.ToInt32(DListDonViTC.SelectedValue = DListDonViTC.SelectedValue != "" ? DListDonViTC.SelectedValue : "0"), Convert.ToInt32(DListDonViGS.SelectedValue = DListDonViGS.SelectedValue != "" ? DListDonViGS.SelectedValue : "0"),
           this.txtNguoiCNC.Text, DateTime.Parse(this.txtNgayCNC.Text.ToString()), this.txtGhiChu.Text, this.txtMaHinhAnh.Text);
        OngNhanhDAL.Update1(OngNhanhEntities);
        //OngNhanhDAL.Update(Convert.ToInt32(txtObjectID.Value), txtMaDoanOng.Text, txtDMA.Text, Convert.ToInt32(DListHDChay.SelectedValue), Convert.ToDouble(this.nChieuDai.Text));
      //  Response.Redirect("../default.aspx");
        ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "<script language='JavaScript'>alert('Cập nhật thành công'); window.location.href ='../Default.aspx';</script>");
    }
    protected void btnHuyBo_Click(object sender, EventArgs e)
    {
        Response.Redirect("../default.aspx");
    }
}